// f[i][j] = max(f[i - 1][j], f[i - 1][j - 1] + w1, f[i - 1][j - 2] + 2w, ... f[i - 1][0] + jw)

#include <iostream>
#include <algorithm>
using namespace std;
const int N = 20;
int w[N][N], f[N][N];
int n, m;

int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j)
            cin >> w[i][j];

    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= m; ++j)
            for (int k = 0; k <= j; ++k)
                f[i][j] = max(f[i][j], f[i - 1][j - k] + w[i][k]);
    }
    cout << f[n][m] << endl;

    int s = m;
    for (int i = n; i >= 1; --i)
        for (int j = s; j >= 0; --j)
        {
            if (f[i][s] == f[i - 1][s - j] + w[i][j])
            {
                cout << i << ' ' << j << endl;
                s -= j;
                break;
            }
        }
    return 0;
}